package com.cdd.agent.config;

import com.alibaba.fastjson2.JSON;
import com.cdd.common.constant.HttpStatus;
import com.cdd.common.core.domain.Result;
import com.cdd.common.utils.ServletUtils;
import com.cdd.common.utils.StringUtils;
import com.cdd.framework.web.service.SysTokenService;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 自定义退出处理类 返回成功
 * 
 */
@Configuration
public class WebLogoutHandlerImpl implements LogoutSuccessHandler
{

    @Resource
    private SysTokenService sysTokenService;

    /**
     * 退出处理
     * 
     * @return
     */
    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
        String token = sysTokenService.getHeaderToken(request);
        if (StringUtils.isNotEmpty(token)) {
            sysTokenService.delLoginUserCache(request);
            //保存登出记录
            sysTokenService.saveLogoutRecord(token);
        }
        ServletUtils.renderString(response, JSON.toJSONString(Result.error(HttpStatus.SUCCESS, "退出成功")));
    }
}
